<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="telephone_type"
                                 placeholder="请选择类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入" @blur="formatTEL"></a-input>
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import moment from 'moment'

export default {
  name: 'TELForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      //用来筛选防重电话的数组
      dataSource: [],
      model: {
        type: '',
        phone: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        type: [
          { required: true, message: '请选择!' }
        ],
        phone: [
          { required: true, message: '请输入!', whitespace: true },
          {
            validator: (rule, value, callback) => {
              console.log('需要校验的对标数组', this.dataSource)
              let fangchong = this.dataSource.find(res => {
                return res.phone == value
              })
              console.log('fangchong', fangchong)
              if (fangchong) {
                callback(new Error('子表单项不满足重复值校验!'))
              }
              let obj = {
                value: value,
                id: this.model.id ? this.model.id : '',
                checkType: 2//客户电话类型
              }
              getAction(this.url.isRepeat, obj).then(res => {
                console.log('重名验证', res)
                if (res.result.noRepeat) {
                  callback('存在重复数据')
                }
                callback()
              })
            },
            trigger: ['blur']
          }
        ]

      },
      url: {
        isRepeat: '/customer/customer/isRepeat',//重名，重号验证
        getNumber: '/sys/common/getNumber',
        add: '/payment/paymentCollection/add',
        edit: '/payment/paymentCollection/edit',
        queryById: '/payment/paymentCollection/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    formatTEL() {
      this.model.phone = this.model.phone.trim()
    },
    moment,
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      that.confirmLoading = true
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = false
          // that.confirmLoading = true
          that.$emit('ok', this.model)

        } else {
          that.confirmLoading = false
        }

      })
    },
    passON(contractAmount) {
      console.log('add收到的数组', contractAmount)
      //回款金额赋值
      this.dataSource = contractAmount
    },
    //编辑用
    passONEdit(contractAmount) {
      //可使用金额
      this.dataSource = contractAmount
    }
  }
}
</script>
<style scoped lang="less">
.w100 {
  width: 100%;
}
</style>